package cn.dgut.training.controller;

import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.goods.CartUpdateDTO;
import cn.dgut.training.emun.CartCheckedEnum;
import cn.dgut.training.dto.goods.CartDTO;
import cn.dgut.training.entity.goods.Cart;
import cn.dgut.training.entity.goods.Goods;
import cn.dgut.training.entity.goods.GoodsProduct;
import cn.dgut.training.vo.goods.CartListVO;
import cn.dgut.training.dto.transaction.Checkout;
import cn.dgut.training.service.ICartService;
import cn.dgut.training.service.IGoodsService;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private ICartService cartService;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private IGoodsService goodsService;

    /**
     * 显示某个用户的购物车信息
     * @param userId
     * @return
     */
    @PostMapping("/index")
    public CommonResponse index(@LoginUser Long userId){
        CartListVO cartListVO =cartService.index(userId);
        return CommonResponse.success(cartListVO);
    }

    /**
     * 返回某用户购物车商品的数量
     * @param userId
     * @return
     */
    @PostMapping("/goodCount")
    public CommonResponse goodCount(@LoginUser Long userId){
        Integer goodCount=cartService.count(new QueryWrapper<Cart>().lambda().eq(Cart::getUserId,userId));
        return CommonResponse.success(goodCount);
    }


    /**
     * 将某商品加入购物车
     * 如果已经存在购物车货品，则增加数量；
     * 否则添加新的购物车货品项。
     * @param cartDTO { goodsId, productId, number,userId }
     * @return
     */
    @PostMapping("/add")
    public CommonResponse add(@LoginUser Long userId,@RequestBody @Valid CartDTO cartDTO){
        cartDTO.setUserId(userId);
        return cartService.addGoods(cartDTO);
    }

    /**
     * 立即购买某商品
     * 如果购物车存在这商品这覆盖数量
     * @param cartDTO
     * @return
     */
    @PostMapping("/fastAdd")
    public CommonResponse fastAdd(@LoginUser Long userId,@RequestBody @Valid CartDTO cartDTO){
        cartDTO.setUserId(userId);
        return cartService.fastAdd(cartDTO);
    }

    /**
     * 购物车商品货品勾选状态
     * @param cartUpdateDTO{ goodsIds: xxx, isChecked: 1/0 ,userId: xxx}
     * @return
     */
    @PostMapping("/checked")
    public CommonResponse checked(@LoginUser Long userId,@RequestBody @Valid CartUpdateDTO cartUpdateDTO){
        cartUpdateDTO.setUserId(userId);
        Long userId1=cartService.checked(cartUpdateDTO);
//        return CommonResponse.message(); //前端处理
        return index(userId1);  //前端重新渲染
    }

    /**
     * 修改购物车商品的数量
     * @param userId
     * @param cartDTO{ id: xxx, goodsId: xxx, productId: xxx, number: xxx }
     * @return
     */
    @PostMapping("/update")
    public CommonResponse updateGoods(@LoginUser Long userId,@RequestBody @Valid CartDTO cartDTO){
        cartDTO.setUserId(userId);
        return cartService.updateGoods(cartDTO);
    }

    /**
     * 得到商品总数
     * @param userId
     * @return
     */
    @GetMapping("/goodsCount")
    public CommonResponse goodsCount(@LoginUser Long userId){
        return CommonResponse.success(cartService.count(new QueryWrapper<Cart>().lambda().eq(Cart::getUserId,userId)));
    }

    /**
     *
     * @param cartDTO{ goodsIds: xxx}
     * @return
     */
    @PostMapping("/delete")
    public CommonResponse deleteCart(@LoginUser Long userId,@RequestBody @Valid CartDTO cartDTO){
        cartDTO.setUserId(userId);
        Long userId1=cartService.deleteGoods(cartDTO);
        //        return CommonResponse.message(); //前端处理
        return index(userId1);  //前端重新渲染
    }

    @PostMapping("/checkout")
    public CommonResponse checkout(@LoginUser Long userId,@RequestBody Checkout checkout){
        return cartService.checkout(userId,checkout);
    }

    @PostMapping("/delBySys")
    public CommonResponse delBySus(@LoginUser Long userId){
       cartService.remove(new QueryWrapper<Cart>()
                .lambda()
                .eq(Cart::getUserId,userId)
                .eq(Cart::getChecked, CartCheckedEnum.IS_CHECKED.getValue()));
       return CommonResponse.message();
    }

    /**
     * 在订单模块中列出已选择的购物车商品
     * @param userId
     * @return
     */
    @PostMapping("/listChecked")
    public List<Cart> listChecked(@RequestParam("userId") Long userId){
        return cartService.list(new QueryWrapper<Cart>()
                .lambda()
                .eq(Cart::getUserId,userId)
                .eq(Cart::getChecked,CartCheckedEnum.IS_CHECKED.getValue()));
    }

    @PostMapping("/deleteChecked")
    public void deleteChecked(@RequestParam("userId") Long userId){
        cartService.remove(new QueryWrapper<Cart>().lambda().eq(Cart::getUserId,userId)
                .eq(Cart::getChecked,CartCheckedEnum.IS_CHECKED.getValue()));
    }

    @PostMapping("/findById")
    public Cart findById(Long cartId){
        return cartService.getById(cartId);
    }

    @GetMapping("/getId")
    public Long getId(@LoginUser Long userId){
        return userId;
    }


}
